
import cv2
import numpy as np
import urllib.request
def url_to_image(url):
    # download the image, convert it to a NumPy array, and then read
    # it into OpenCV format
    resp = urllib.request.urlopen(url)
    # bytearray将数据转换成（返回）一个新的字节数组
    # asarray 复制数据，将结构化数据转换成ndarray
    image = np.asarray(bytearray(resp.read()), dtype="uint8")
    # cv2.imdecode()函数将数据解码成Opencv图像格式
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)
    # return the image
    return image

def preprocess_foam(image, INPUT_H,INPUT_W):
    image = cv2.resize(image, (INPUT_W,INPUT_H))
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = np.transpose(np.array(image, dtype=np.float32, order='C'), (2, 0, 1))
    image /= 255.0
    # print(image,image.shape)
    #???
    return image
 

def process_cls_result(output):#res是出来的，output是进去的
    # for i in range(INPUT_W*INPUT_H*1):
    #     res.mask[i] = sigmoid(output)
    return 1/(1 + np.exp(-output))


def softmax(x):
# 计算每行的最大值
    return (np.exp(x)/np.sum(np.exp(x),axis=0))